/*
 * File:   Search.cpp
 * Author: joseluis
 *
 * Created on 26 de diciembre de 2012, 11:41
 */

#include <qt4/QtGui/qpushbutton.h>


#include <qt4/Qt/qlabel.h>
#include <qt4/Qt/qlistwidget.h>
#include <qt4/Qt/qboxlayout.h>

#include "Search.h"
#include "IdDocs.h"
#include "Question.h"

Search::Search(int argc, char *argv[]) {
    AgentDB::getAgentePtr()->create_database(argc, argv);
    widget.setupUi(this);
    sol = new vector<InfoDoc > ();
    it = sol->begin();
}

Search::~Search() {
}

void Search::lupa_clicked() {
    Consulta(widget.pregunta->text().toStdString(), widget.checkBox->isChecked());
}

void Search::similars_clicked() {
    if (!widget.listWidget->selectedItems().isEmpty()) {
        std::string s_file = widget.listWidget->currentItem()->toolTip().toStdString();
        Question* q = new Question();
        Consulta(q->similarQuestion(s_file), widget.checkBox->isChecked());
        delete q;
    }
}

void Search::add_widget(InfoDoc entry) {
    PersonalWidget* pw = new PersonalWidget(widget.listWidget, &entry);
    QListWidgetItem* aux = new QListWidgetItem();
    aux->setText("\n\n\n\n");
    aux->setToolTip(entry.File.c_str());
    widget.listWidget->addItem(aux);
    widget.listWidget->setItemWidget(aux, pw);
}

void Search::Consulta(std::string question, bool checked) {
    widget.listWidget->clear();
    Question* q = new Question();
    sol = q->makeQuestion(question, checked);
    it = sol->begin();
    show_res();
    delete q;
}

void Search::add_clicked() {
    if (it != sol->end()) {
        show_res();
    }
}

void Search::show_res() {
    for (int i = 0; i < 25 && it != sol->end(); i++) {
        add_widget(*it);
        it++;
    }
}

